package logic

import (
	"context"
	"fmt"
	"furni-home/model"

	"furni-home/homerpc/homerpc"
	"furni-home/homerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShowHomeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShowHomeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShowHomeLogic {
	return &ShowHomeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShowHomeLogic) ShowHome(in *homerpc.ShowHomeRequest) (*homerpc.ShowHomeResponse, error) {
	// todo: add your logic here and delete this line
	var home model.HomeData
	err := model.DB.Debug().Model(&model.Home{}).
		Select("homes.*,classes.`name` AS class_name,brands.`name` AS brand_name").
		Where("homes.id=?", in.HomeId).
		Joins("LEFT JOIN classes ON classes.id = homes.class_id").
		Joins("LEFT JOIN brands ON brands.id = homes.brand_id").
		Find(&home).Error
	if err != nil {
		return nil, fmt.Errorf("详情查询失败")
	}
	return &homerpc.ShowHomeResponse{
		Name:      home.Name,
		Price:     float32(home.Price),
		Stock:     home.Stock,
		Image:     home.Image,
		ClassName: home.ClassName,
		BrandName: home.BrandName,
		HomeId:    int64(home.ID),
	}, nil
}
